<?php

namespace ServiceSdk\Payment\Payunk;

use ServiceSdk\Libs\Remote;

class Notify extends Config
{

    /**
     * 解析通知数据
     *
     * @param array $payInfo
     *
     * @return array
     */
    public static function parseNotifyData(array $callbackData)
    {
        $return = [
            'error'   => false,
            'message' => '',
            'data'    => []
        ];
        if (isset($callbackData['callbacks']) && $callbackData['callbacks'] === 'CODE_SUCCESS') {
            if (self::verifySign($callbackData)) {
                // 验签成功
                $return['data'] = [
                    'order_sn' => $callbackData['out_trade_no'],
                    'amount'   => $callbackData['amount'],
                    'pay_type' => $callbackData['pay_type'],
                ];
            } else {
                $return['error'] = true;
                $return['message'] = '支付签证失败';
            }
        } else {
            $return['error'] = true;
            $return['message'] = '支付渠道上报错误信息';
        }

        return $return;
    }

    /**
     * @Note   验证签名
     *
     * @param $data
     * @param $orderStatus
     *
     * @return bool
     */
    private static function verifySign($data): bool
    {
        // 验证参数中是否有签名
        if (! isset($data['sign']) || ! $data['sign']) {
            return false;
        }
        // 要验证的签名串
        $sign = $data['sign'];
        unset($data['sign']);
        // 生成新的签名、验证传过来的签名
        $sign2 = self::getSign($data);

        return $sign === $sign2;
    }

}
